/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package monety;

/**
 *
 * @author Jedrek
 */
public class Coin {
    
    private String name;
    int year;
    private int count;
    
    private float[] histogram;    //25 liter + jedno miejsce na liczbe innych znaczkow
    private float[][] markov;
    
    public Coin(){
        count = 1;
        histogram = new float[27];
        markov = new float[27][27];
    }
    
    public Coin(String name, int year){
        this.name = name;
        this.year = year;
        count = 1;
        histogram = new float[27];
        markov = new float[27][27];
        
        calcHistogram();
        calcMarkov();
    }
    
    @Override
    public String toString(){
        return new String(name);
    }
    
    public int getCount(){
        return count;
    }
    
    public boolean setCount(int count){
        if( count < 0 )
            return false;
        
        this.count = count;
        return true;
    }
    
    public void incCount(){
        count++;
    }
    
    public boolean decCount(){
        if( count == 0 )
            return false;
        
        count--;
        return true;
    }
    
    public void setName(String name){
        this.name = name;
        calcHistogram();
        calcMarkov();
    }
    
    public String getName(){
        return name;
    }
    
    public void calcHistogram(){
        char[] sName = name.toLowerCase().toCharArray();
        
        for(int i=0; i<27; i++)
            histogram[i] = 0;
        
        for(int i=0; i<sName.length; i++){
            int letter = (int)sName[i] - 'a';
            
            if(letter>25 || letter<0){
                histogram[26]+=1;
                continue;
            }
            
            histogram[letter]+=1;
        }

        //normalizacja
        double suma = 0;
        for(int i = 0; i<27; i++){
            suma+=histogram[i]*histogram[i];
        }
        suma = Math.sqrt(suma);
        for(int i = 0; i<27; i++){
            histogram[i] = (float)(histogram[i]/suma);
        }
    }
    
    public float[] getHistogram(){
        return histogram.clone();
    }
        
    public float compareHist(float[] hist2){
        float cor = 0;

        if(hist2.length != histogram.length)
            return 0;

        for(int i = 0; i<hist2.length; i++){
            cor+= histogram[i]*hist2[i];
        }

        return cor;
    }
    
    public float compareYear(int year){
        return 1.0f/(1.0f + (float)Math.pow(Math.abs(year-this.year), 2));
    }
    
    public void calcMarkov(){
        char[] sName = name.toLowerCase().toCharArray();
        
        for(int i=0; i<27; i++)
            for(int j = 0; j<27; j++)
                markov[i][j] = 0;
        
        for(int i=0; i<sName.length-1; i++){
            int letter1 = (int)sName[i] - 'a';
            int letter2 = (int)sName[i+1] - 'a';
            
            if(letter1>25 || letter1<0){
                letter1 = 26;
            }
            if(letter2>25 || letter2<0){
                letter2 = 26;
            }
            
            markov[letter1][letter2]+=1;
        }
        
        //normalizacja
        double suma = 0;
        for(int i = 0; i<27; i++)
            for(int j = 0; j<27; j++)
                suma+=markov[i][j]*markov[i][j];
        suma = Math.sqrt(suma);
        for(int i = 0; i<27; i++)
            for(int j = 0; j<27; j++)
                markov[i][j] = (float)(markov[i][j]/suma);
    }
    
    public float[][] getMarkov(){
        return markov.clone();
    }
    
    public float compareMarkov(float[][] markov2){
        float cor = 0;

        if(markov2.length != histogram.length)
            return 0;

        for(int i = 0; i<markov2.length; i++)
            for(int j = 0; j<markov2.length; j++){
                if(markov[i].length != markov2[i].length)
                    return 0;
                cor+= markov[i][j]*markov2[i][j];
            }
        
        return cor;
    }
}
